<template>
  <div class="home">
    <!-- 购物车按钮头部 -->
    <div class="home_header">
      <div class="home_header_button" @click="toCart">
        admin Logout
        <van-icon name="shopping-cart-o" size="24" />
      </div>
    </div>
    <!-- 页面面包屑导航栏 -->
    <div class="home_route">
      <el-breadcrumb separator="/" class="home_route_wrapper">
        <el-breadcrumb-item :to="{ path: '/' }">Home</el-breadcrumb-item>
        <el-breadcrumb-item class="home_route_red">Goods</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 下面部分 -->
    <div class="home_main_container">
      <div class="home_main_wrapper">
        <!-- 下面头部 -->
        <div class="home_main_header">
          <div class="main_sort_container">
            Sort by:
            <span @click="onSort">{{sort?'Low To Hight':'Hight To Low'}}</span> Price
            <!-- <van-icon name="down" /> -->
          </div>
        </div>
        <!-- 下面内容盒子 -->
        <div class="main_content">
          <el-container>
            <!-- 左侧边栏 -->
            <el-aside width="260px" class="left_block">
              <div class="left_slider_container">
                <div class="left_slider_title">Price:</div>
                <div
                  :class="selectIndex === index?'left_slider_item_active':'left_slider_item'"
                  v-for="(item,index) in slider"
                  :key="index"
                  @click="onSelectIndex(index)"
                >{{item.title}}</div>
              </div>
            </el-aside>
            <!-- 右侧内容栏 -->
            <el-main class="main_padding">
              <div class="list_items_container">
                <el-row :gutter="20" justify="start">
                  <el-col
                    :xs="24"
                    :sm="12"
                    :md="8"
                    :lg="6"
                    class="element_item"
                    v-for="(item,index) in showList"
                    :key="index"
                  >
                    <div class="grid-content bg-purple list_item">
                      <div class="item_img">
                        <img :src="item.productImage" alt />
                      </div>
                      <div class="item_title">{{item.productName}}</div>
                      <div class="item_price">￥{{item.salePrice}}</div>
                      <div class="item_button" @click="addCart(item)">加入购物车</div>
                    </div>
                  </el-col>
                </el-row>
              </div>
            </el-main>
          </el-container>
        </div>
      </div>
    </div>
    <!-- 加载动画 -->
    <div class="loading_container" v-myShow="isShow">
      <img src="../assets/loading.gif" alt />
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Home",
  components: {},
  data() {
    return {
      slider: [],
      listItems: [],
      showList:[],
      selectIndex: 0,
      sort: true,
      isShow:false,
      page:2,
      cartItems:[]
    };
  },
  computed: {

  },
  methods: {
    onSelectIndex(index) {
      this.selectIndex = index;
      this.showList = []
      var selectItem = this.slider[this.selectIndex];
      this.listItems.forEach(ele => {
        // console.log(selectItem)
        if (
          ele.salePrice > selectItem.low &&
          ele.salePrice < selectItem.hight
        ) {
          this.showList.push(ele);
        }
      });
      this.listItems.sort((a, b) => {
        return this.sort
          ? a.salePrice - b.salePrice
          : b.salePrice - a.salePrice;
      });

    },
    toCart(){
      this.$router.push('/cart')
    },
    onSort() {
      this.sort = !this.sort;
    },
    addCart(item){
      // console.log(item)

      this.$store.dispatch('ADD_CART',item)
    }
  },
  directives:{
    myShow:{
      bind(){

      },
      inserted(el,binding){
        if(binding.value){
          el.style.visibility = "visible"
        }else {
          el.style.visibility = "hidden"
        }
      },
      update(el,binding){
        if(binding.value){
          el.style.visibility = "visible"
        }else {
          el.style.visibility = "hidden"
        }
      }
    }
  },
  mounted() {
    // 添加滚动监听
    window.addEventListener("scroll", () => {
      // 可视区域高度
      var lookHeight = document.documentElement.clientHeight;
      // 滚动高度
      var scrollHeight = document.documentElement.scrollTop;
      // 文档高度
      var documentHeight = document.documentElement.scrollHeight;
      // console.log(lookHeight);
      // console.log(parseInt(scrollHeight));
      // console.log(documentHeight-lookHeight-parseInt(scrollHeight));
      
      
      if(documentHeight-lookHeight-parseInt(scrollHeight) === 0){
        console.log('已经滚到底部了......')
        this.isShow = true
        if(this.page === 4){
          this.isShow = false
          return
        }
        window.setTimeout(()=>{
          this.isShow = false
          axios.get(`http://localhost:8080/data${this.page}.json`).then((res)=>{
            // console.log(res.data.result.list)
            this.showList = [...this.showList,...res.data.result.list]
            this.listItems = [...this.listItems,...res.data.result.list]
          }).catch((error)=>{
            console.log(error)
          })
          this.page++
        },3000)
        console.log(123123123132,this.showList)
        // page++
      }
    });

    // 左侧边栏价格筛选数据与右边商品数据获取
    axios
      .get("http://localhost:8080/data.json")
      .then(res => {
        // console.log(res.data.result);
        this.slider = res.data.result.slider;
        this.listItems = res.data.result.list;
        this.showList = res.data.result.list;
      })
      .catch(error => {
        console.log(error);
      });
  }
};
</script>

<style scoped>
.home {
  width: 100%;
}
.home_header {
  width: 100%;
  height: 50px;
  display: inline-flex;
  justify-content: flex-end;
  align-items: center;
}
.home_header_button {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  margin-right: 5%;
}
.home_route {
  width: 100%;
  height: 60px;
  background-color: #eee;
  display: inline-flex;
  justify-content: flex-start;
  align-items: center;
  font-size: 20px;
}
.home_route_wrapper {
  margin-left: 5%;
}
.home_route_red {
  color: red;
}
.home_main_container {
  width: 100%;
  background-color: #f5f7fb;
  display: inline-flex;
  justify-content: center;
  align-items: center;
}
.home_main_wrapper {
  width: 90%;
}
.home_main_header {
  width: 100%;
  height: 60px;
  display: inline-flex;
  justify-content: flex-end;
  align-items: center;
  background-color: #fff;
  margin: 40px 0;
}
.main_sort_container {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  margin-right: 20px;
}
.main_sort_container span {
  margin: 0px 20px;
  color: orange;
}
.main_sort_container span:hover {
  cursor: pointer;
  color: #007acc;
}
.main_content {
  width: 100%;
}
.left_slider_container {
  width: 100%;
  height: 100%;
  /* background-color: #fff; */
}
.left_slider_title {
  font-weight: 700;
  width: 85%;
  height: 40px;
  display: inline-flex;
  justify-content: flex-start;
  align-items: center;
  margin-left: 10%;
  margin-top: 20px;
}
.left_slider_item {
  color: gray;
  width: 85%;
  height: 30px;
  margin: 10px 0;
  margin-left: 10%;
  display: inline-flex;
  justify-content: flex-start;
  align-items: center;
  border-left: 3px solid #fff;
}
.left_slider_item_active {
  width: 85%;
  height: 30px;
  margin: 10px 0;
  margin-left: 10%;
  display: inline-flex;
  justify-content: flex-start;
  align-items: center;
  color: orange;
  text-indent: 10px;
  border-left: 3px solid red;
}
.left_slider_item:hover {
  cursor: pointer;
  color: orange;
  text-indent: 10px;
}
.left_slider_item_active {
  cursor: pointer;
  color: orange;
  text-indent: 10px;
}
.list_items_container {
  width: 100%;
}
.list_item {
  /* width: 100%; */
  margin: 10px 0;
  background-color: #fff;
}
.item_img {
  width: 100%;
}
.list_item img {
  width: 100%;
}
.item_price {
  width: 100%;
  color: red;
  margin: 10px 0;
  text-indent: 5%;
}
.item_title {
  width: 100%;
  margin: 0px 0;
  text-indent: 5%;
}
.item_button {
  width: 90%;
  margin-left: 5%;
  height: 40px;
  border: 1px solid red;
  margin: 5%;
  display: inline-flex;
  justify-content: center;
  align-items: center;
}
.main_padding {
  padding: 0;
}
.home_header_button:hover {
  color: #007acc;
  cursor: pointer;
}
.list_item:hover {
  cursor: pointer;
  box-shadow: 0px 3px 5px #000;
  transition: all 0.3s ease-in-out;
  transform: translateY(-5px);
}
.loading_container {
  width: 100%;
  height: 100px;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  background-color: #fff;
}
.loading_container img {
  height: 100%;
}

@media screen and (max-width: 768px) {
  .left_block {
    display: none;
  }
  .list_item {
    width: 100%;
    height: 150px;
    margin: 20px 0;
    display: inline-flex;
    justify-content: center;
    align-items: center;
  }
  .home_main_header {
    justify-content: flex-start;
  }
}
</style>